<template>
  <div class="faculty-container">
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="searchForm" class="search-form">
      <el-form-item label="高校名称">
        <el-select v-model="searchForm.universityId" placeholder="请选择高校" filterable>
          <el-option
            v-for="item in universities"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="师资类型">
        <el-select v-model="searchForm.facultyType" placeholder="请选择师资类型">
          <el-option label="院士" value="院士"></el-option>
          <el-option label="教授" value="教授"></el-option>
          <el-option label="副教授" value="副教授"></el-option>
          <el-option label="博士生导师" value="博士生导师"></el-option>
          <el-option label="硕士生导师" value="硕士生导师"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="handleAdd">新增师资</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="universityName" label="高校名称" width="200"></el-table-column>
      <el-table-column prop="facultyType" label="师资类型" width="150"></el-table-column>
      <el-table-column prop="facultyCount" label="数量"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total">
      </el-pagination>
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="高校" prop="universityId">
          <el-select v-model="form.universityId" placeholder="请选择高校" filterable>
            <el-option
              v-for="item in universities"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="师资类型" prop="facultyType">
          <el-select v-model="form.facultyType" placeholder="请选择师资类型">
            <el-option label="院士" value="院士"></el-option>
            <el-option label="教授" value="教授"></el-option>
            <el-option label="副教授" value="副教授"></el-option>
            <el-option label="博士生导师" value="博士生导师"></el-option>
            <el-option label="硕士生导师" value="硕士生导师"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="facultyCount">
          <el-input-number v-model="form.facultyCount" :min="0" :max="9999"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Faculty',
  data() {
    return {
      universities: [], // 高校列表
      searchForm: {
        universityId: '',
        facultyType: ''
      },
      tableData: [],
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      dialogVisible: false,
      dialogTitle: '新增师资',
      form: {
        universityId: '',
        facultyType: '',
        facultyCount: 0
      },
      rules: {
        universityId: [
          { required: true, message: '请选择高校', trigger: 'change' }
        ],
        facultyType: [
          { required: true, message: '请选择师资类型', trigger: 'change' }
        ],
        facultyCount: [
          { required: true, message: '请输入数量', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchUniversities()
    this.fetchData()
  },
  methods: {
    // 获取高校列表
    async fetchUniversities() {
      try {
        const res = await this.$axios.get('/academicPrograms/university/all')
        this.universities = res.data
      } catch (error) {
        this.$message.error('获取高校列表失败')
      }
    },
    // 获取数据
    async fetchData() {
      try {
        const params = {
          page: this.pagination.currentPage,
          size: this.pagination.pageSize,
          universityId: this.searchForm.universityId,
          facultyType: this.searchForm.facultyType
        }
        const res = await this.$axios.get('/faculty/list', { params })
        this.tableData = res.data.records
        this.pagination.total = res.data.total
      } catch (error) {
        this.$message.error('获取数据失败')
      }
    },
    // 搜索
    handleSearch() {
      this.pagination.currentPage = 1
      this.fetchData()
    },
    // 重置搜索
    resetSearch() {
      this.searchForm = {
        universityId: '',
        facultyType: ''
      }
      this.handleSearch()
    },
    // 新增
    handleAdd() {
      this.dialogTitle = '新增师资'
      this.form = {
        universityId: '',
        facultyType: '',
        facultyCount: 0
      }
      this.dialogVisible = true
    },
    // 编辑
    handleEdit(row) {
      this.dialogTitle = '编辑师资'
      this.form = { ...row }
      this.dialogVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确认删除该师资信息吗？', '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          await this.$axios.delete(`/faculty/delete/${row.id}`)
          this.$message.success('删除成功')
          this.fetchData()
        } catch (error) {
          this.$message.error('删除失败')
        }
      }).catch(() => {})
    },
    // 提交表单
    handleSubmit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.form.id) {
              // 编辑
              await this.$axios.put('/faculty/update', this.form)
              this.$message.success('更新成功')
            } else {
              // 新增
              await this.$axios.post('/faculty/add', this.form)
              this.$message.success('添加成功')
            }
            this.dialogVisible = false
            this.fetchData()
          } catch (error) {
            this.$message.error(error.message || '操作失败')
          }
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.faculty-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
  background-color: #f5f7fa;
  padding: 20px;
  border-radius: 4px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style> 